<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>添加权限资源</title>
<link href="__PUBLIC__/admin/css/xc.min.css" rel="stylesheet" type="text/css" />
<link href="__PUBLIC__/admin/css/xc.admin.css" rel="stylesheet" type="text/css" />
<link href="__PUBLIC__/lib/iconfont/iconfont.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="__PUBLIC__/admin/js/jquery.min.js"></script>
<script type="text/javascript" src="__PUBLIC__/admin/js/xc.js"></script> 
<script type="text/javascript" src="__PUBLIC__/admin/js/xc.admin.js"></script>
<script type="text/javascript" src="__PUBLIC__/admin/js/common-1.0.js"></script>
<script type="text/javascript" src="__PUBLIC__/lib/layer/layer.js"></script>
</head>
<body>
<div class="pd-20">
  <form action="" method="post" class="form form-horizontal" id="myform">
    <div class="row cl">
      <label class="form-label col-3"><span class="c-red">*</span>权限名称：</label>
      <div class="formControls col-8">
        <input type="text" id="rname" name="rname" value="{$info.me_name}" class="input-text" placeholder="请输入权限名称">
      </div>
    </div>
    <div class="row cl">
      <label class="form-label col-3"><span class="c-red">*</span>所属分组：</label>
      <div class="formControls col-8"> <span class="select-box">
        <select class="select" id="group" name="group">
        <foreach name='rtype' item='vo' key='k'>
          <option value="{$k}" <if condition="($info[me_group] eq $k)">selected</if> >{$vo}</option>
        </foreach>
        </select>
      </span></div>
    </div>
    <div class="row cl">
      <label class="form-label col-3"><span class="c-red">*</span>权限码：</label>
      <div class="formControls col-8">
        <span class="select-box mr-5 l" style="width:40%">
          <select class="select" id="controller" onchange="get_act_list(this)">
            <option value="">选择控制器</option>
            <foreach name="planList" item="vo">
            <option value="{$vo}">{$vo}</option>
            </foreach>     
          </select>
        </span><span class="mr-5 l"> @ </span>
        <span class="select-box mr-5 l" style="width:40%">
          <select class="select" id="act_list"></select>
        </span>
        <div class="formControls mr-5 l"><input type="button" value="添加" onclick="add_right()" class="btn btn-primary radius"></div>    
      </div>
    </div>
    <div class="row cl">
      <label class="form-label col-3"></label>
      <div class="formControls col-8">
      <table class="table table-border table-bordered table-hover table-bg table-sort">
        <thead>
    	  <tr class="text-c"><th style="width:80%">权限码</th><th>操作</th></tr>
        </thead>
        <tbody id="rightList">
          <foreach name="info[right]" item="vo">
          <tr>
            <td><input name="right[]" type="text" value="{$vo}" class="input-text"></td>
            <td><a href="javascript:;" onclick="$(this).parent().parent().remove();" class="btn btn-danger radius">删除</a></td>
          </tr>
          </foreach>
        </tbody>
      </table>
      </div>
    </div>
    <div class="row cl">
      <div class="col-9 col-offset-3">
        <input type="hidden" name="id" value="{$info.me_id}">
        <button type="button" id="sub" name="sub" class="btn btn-primary radius" onClick="checkSubmit()"><i class='Hui-iconfont'>&#xe632;</i> 保存</button>
        <button class="btn btn-default radius" type="button" onClick="layer_close();"><i class='Hui-iconfont'>&#xe66b;</i> 取消</button>
      </div>
    </div>
  </form>
</div>
<script type="text/javascript">
//按回车件触发提交
document.onkeydown=function(event){
	var e = event || window.event || arguments.callee.caller.arguments[0];
	if(e && e.keyCode==13){ // enter 键
		checkSubmit();
	}
};
function checkSubmit(){
	var rname = $.trim($('#rname').val());
	var rcode = $('input[name="right[]"').val();
	if(rname == ''){
		layer.msg('权限名称不能为空！', {icon: 2,time: 1000});
		return false;
	}if(rcode == '' || rcode == null){
		layer.msg('权限码不能为空！', {icon: 2,time: 1000});
		return false;
	}
	$.ajax({
		type : 'post',
		url : '__MODULE__/Role/roleAddEdit',
		data : $("#myform").serialize(),
		dataType : 'json',
		success : function(res){
			if(res.status == 1){
				layer.msg(res.msg,{icon:1,time:2000},function(){
					// 先获取窗口索引，才能关闭窗口  
					var index = parent.layer.getFrameIndex(window.name);
					parent.layer.close(index);
				});
				parent.call_back_fun('success');
			}else{
				layer.msg(res.msg, {icon: 2});
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			layer.alert('网络失败，请刷新页面后重试',{icon:2});
		}
	})
}
//添加相关权限码条数
function add_right(){
	var a = [];
	$('#rightList .input-text').each(function(i,o){
		if($(o).val() != ''){
			a.push($(o).val());
		}
	})
	var ncode = $('#controller').val();
	var vcode = $('#act_list').val();
	if(ncode !== ''){
		if(vcode == ''){
			layer.msg('方法名不能为空！', {icon: 2,time: 1000});
			return false;
		}
		var temp = ncode+'@'+ vcode;
		if($.inArray(temp,a) != -1){
			layer.msg('此权限码已经添加！', {icon: 2,time: 1000});
			return false;
		}
	}else{
		layer.msg('请选择控制器！', {icon: 2,time: 1000});
		return false;
	}
	var strtr = '<tr>';
	if(ncode!= ''){
		strtr += '<td><input type="text" name="right[]" value="'+ncode+'@'+ vcode+'" class="input-text" style="width:400px;"></td>';
	}else{
		strtr += '<td><input type="text" name="right[]" value="" class="input-text" style="width:400px;"></td>';
	}		
	strtr += '<td class="text-c"><a href="javascript:;" onclick="$(this).parent().parent().remove();" class="btn btn-danger radius">删除</a></td>';
	$('#rightList').append(strtr);	
}
//获取控制器中相关方法名
function get_act_list(obj){
	$.ajax({
		url: "{:U('Role/ajax_get_action')}",
		type:'post',
		data: {'controller':$(obj).val()},
		dataType:'html',
		success:function(res){
			$('#act_list').empty().append(res);
		}
	});
}
</script>
</body>
</html>